package com.bskj.common.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间工具类
 * 
 * @author lishi
 * @date 2018年4月12日 下午12:45:43
 */
public class DateTimeUtil {
	public static String date2String(Date date) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date) + "";
	}

	public static String date2String(Date date, String format) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date) + "";
	}

	public static Date string2Date(String str) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.parse(str);
	}

	public static Date string2Date(String str, String format) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(str);
	}
	
	public static String getDisBetweenTimes(Date beforeTime,Date afterTime)throws Exception{
		long milos = afterTime.getTime() - beforeTime.getTime();
		milos = milos / 1000;
		if(milos < 60) {
			return "小于1分钟";
		}
		
		if(milos < 60 * 60) {
			return (milos / 60) + "分钟";
		}
		
		if(milos < 60 * 60 * 24) {
			int hours = (int)(milos / 3600);
			int minutes =(int)((milos % 3600) / 60);
			return hours + "小时 " + minutes + "分钟";
		}
		
		int day = (int)(milos / (3600 * 24));
		milos = milos % (3600 * 24);
		int hours = (int)(milos / 3600);
		int minutes =(int)((milos % 3600) / 60);
		return day + "天 " + hours + "小时 " + minutes + "分钟";
	}
}
